﻿using System;
using System.Collections.Generic;
using Rackspace.Cloudfiles;

namespace rackspacecloudman
{
    class Entry
    {
        private static string ContainerName;
        private const string _index = "index.htm";
        private const string _error = "index.htm";

        static void Main()
        {
            string username = Properties.Settings.Default.username;
            string api_key = Properties.Settings.Default.api_key;
            string auth_url = Properties.Settings.Default.auth_url;

            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("Please enter your username:");
                username = Console.ReadLine();
            }

            if (string.IsNullOrEmpty(api_key))
            {
                Console.WriteLine("Please enter your API Key:");
                api_key = Console.ReadLine();
            }

            Console.WriteLine("Enter domain name (do not include www.)");
            string domainName = Console.ReadLine();

            ContainerName = domainName + "_www";

            var creds = new UserCredentials(username, api_key, auth_url);
            var client = new CF_Client();

            Connection conn = new CF_Connection(creds, client);
            Console.WriteLine("Authenticating...");
            conn.Authenticate();
            Console.WriteLine("Authenticated");




            CreateContainer(conn, client);

            ListAllContainers(conn, client);

            Console.ReadLine();

        }

        private static void CreateContainer(Connection conn, Client client)
        {
            var account = new CF_Account(conn, client);
            Dictionary<string, string> containerMeta = new Dictionary<string, string>();
            containerMeta.Add("X-Container-Meta-Web-Error", _error);
            containerMeta.Add("X-Container-Meta-Web-Index", _index);

            Console.WriteLine("Creating container {0}", ContainerName);
            account.CreateContainer(ContainerName, containerMeta);

            Console.WriteLine("Successfully created container {0}", ContainerName);
            BreakLine();
        }


        private static void ListAllContainers(Connection conn, Client client)
        {
            var account = new CF_Account(conn, client);
            var list = account.GetContainers(true);
            Console.WriteLine("There are {0} containers in the account", list.Count);
            Console.WriteLine("Listing all containers");
            foreach (var item in list)
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("Listing all containers complete");
            BreakLine();
        }

        private static void BreakLine()
        {
            Console.WriteLine("========================================================");
            Console.WriteLine("========================================================");
        }
    }
}
